using System;

namespace RapidHDL
{
	/// <summary>
	/// Summary description for Conversion.
	/// </summary>
	public class Conversion
	{
		public Conversion()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/*public static NodeVector BuildStateVector(string psStateString)
		{
			int iIndex;
			string sStateString = psStateString.ToUpper().Trim();
			char cValue;
			Node ndNode;

			NodeVector nvNodeVector = new NodeVector(sStateString.Length);
			for(iIndex = 0; iIndex < sStateString.Length; iIndex++)
			{
				ndNode = nvNodeVector[iIndex];
				cValue = sStateString[iIndex];
				switch(cValue)
				{
					case 'X' :
						ndNode.NodeState = NodeState.Undefined;
						break;
					case '1' :
						ndNode.NodeState = NodeState.High;
						break;
					case '0' :
						ndNode.NodeState = NodeState.Low;
						break;
					case 'Z' :
						ndNode.NodeState = NodeState.Tri;
						break;
					default:
						System.Diagnostics.Debug.Assert(false,"Bad State Vector");
						break;
				}
			}
			return nvNodeVector;
		}*/

		public static string IntToBinaryString(int piInputInt, int piLength)
		{
			string sBinary;
			
			int iQ = piInputInt / 2;
			int iR = piInputInt % 2;
			
			if(iR == 1)
				sBinary = "1";
			else
				sBinary = "0";

			while(iQ > 0)
			{
				iR = iQ % 2;
				iQ = iQ / 2;
				
				if(iR == 0)
					sBinary = "0" + sBinary;
				else
					sBinary = "1" + sBinary;
			}

			if (piLength != 0)
			{
				sBinary = sBinary.PadLeft(piLength,'0');
				return sBinary.Substring(sBinary.Length - piLength,piLength);
			}
			else
			{
				return sBinary;
			}
		}

		public static string IntToBinaryString(int piInputInt)
		{
			return IntToBinaryString(piInputInt,0);
		}

		/*public static NodeVector CombineVectors(params NodeVector[] nvInputVectors)
		{
			int iFinalSize;
			NodeVector nvOutputVector;

			foreach (NodeVector nvInputVector in nvInputVectors)
			{
				iFinalSize += nvInputVector.Size;
			}

			nvOutputVector = new NodeVector(iFinalSize);
			foreach (NodeVector nvInputVector in nvInputVectors)
			{
				nvInputVector.Append();
			}

		}*/

		public static string NodeVectorsToString(System.Collections.Hashtable poNodeVectors)
		{
			string sResult = "";
			foreach(NodeVector oNodeVector in poNodeVectors.Values)
			{
				sResult += oNodeVector.NodeVectorAsString;
			}
			return sResult;
		}

		public static void AssignNodeVectorsFromString(System.Collections.Hashtable poNodeVectors, string psValue)
		{
			int iIndex = 0;

			if(NodeVectorsCountNodes(poNodeVectors) != psValue.Length)
				System.Diagnostics.Debug.Fail("NodeVectors size doesn't match string size");

			foreach(NodeVector oNodeVector in poNodeVectors.Values)
			{
				oNodeVector.NodeVectorAsString = psValue.Substring(iIndex,oNodeVector.Size);
				iIndex += oNodeVector.Size;
			}
		}

		public static int NodeVectorsCountNodes(System.Collections.Hashtable poNodeVectors)
		{
			int iResult = 0;
			foreach(NodeVector oNodeVector in poNodeVectors.Values)
			{
				iResult += oNodeVector.Nodes.Count;
			}
			return iResult;
		}
	}
}
